package org.dizem.intellipainter.component.color;

import org.dizem.intellipainter.MainFrame;
import org.dizem.intellipainter.util.AppletUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * User: dizem@126.com
 * Time: 12-2-18 下午4:02
 */
public class PickColorButton extends JButton {
	private static final Font font = new Font("微软雅黑", Font.PLAIN, 12);
	private static final Dimension size = new Dimension(55, 70);
	private Color color = Color.WHITE;
	private Image image = null;

	public PickColorButton(final MainFrame mainFrame, final ColorPickCallback callback) {
		setDoubleBuffered(true);
		setFont(font);
		addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				color = JColorChooser.showDialog(mainFrame, "编辑颜色", color);
				callback.onSelectColor(color);
			}
		});
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.setRenderingHints(qualityHints);
		g.drawString("编辑颜色", 4, 60);

		if (image != null && image.getWidth(null) > 0) {
			g.drawImage(image, 15, 13, null);
		}
	}

	@Override
	public Dimension getPreferredSize() {
		return size;
	}

	public void init(JApplet applet) {
		image = AppletUtil.getImage(applet, "pickColor.png");
	}
}
